package com.kehutong.crm.app;

import java.util.List;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.execute.entity.FindArray;
import org.coraframework.util.Objects;
import com.kehutong.common.entity.Root;
import com.kehutong.common.util.Token;
import com.kehutong.crm.entity.WordsOwned;
import com.kehutong.crm.entity.WordsShare;

/**
 * 话术库
 *
 * @Author: liuzhen (liuxing521a@163.com)
 * @Date: 20/4/15 下午4:52
 */
@MvcAction
@ReqMapping("/kht-bin/words")
public class WordsAction {

	@Inject
	private JdbcSession jdbcSession;

    /**
     * 公共话术
     * @param jsonObject 参数对象
     * @return 公共话术列表
     */
    @ReqMapping("/listWordsShare")
    public Object listDiscourse(JSONObject jsonObject) {
        final String pid = jsonObject.getString("pid");
        final FindArray<WordsShare> find = jdbcSession.findArray(WordsShare.class)
                .eq("isFolder", false)
                .eq("deleted", false)
                .like(jsonObject, "name")
                .eq("pid",pid);
        List<WordsShare> wordsList = find.exe();
        return Result.success(wordsList);
    }

    @ReqMapping("/folder/list")
    public Object folderList(JSONObject jsonObject){
        List<WordsShare> folderList = jdbcSession.findArray(WordsShare.class)
                                                 .eq(jsonObject, "pid")
                                                 .like(jsonObject, "name")
                                                 .eq("isFolder", true)
                                                 .eq("deleted", false)
                                                 .exe();
        return Result.success(folderList);
    }

    @ReqMapping("/search")
    public Object search(JSONObject jsonObject){
        List<WordsShare> folderList = jdbcSession.findArray(WordsShare.class)
                                                 .eq(jsonObject, "pid")
                                                 .like(jsonObject, "name")
                                                 .eq("isFolder", false)
                                                 .eq("deleted", false)
                                                 .exe();
        return Result.success(folderList);
    }

    /**
     * 个人话术
     * @param token 用户信息
     * @param jsonObject 参数对象
     * @return 个人话术列表
     */
    @ReqMapping("/listWordsOwner")
    public Object listWordsOwner(Token token, JSONObject jsonObject) {
        final List<WordsOwned> wordsList = jdbcSession.findArray(WordsOwned.class)
                .eq(jsonObject, "companyNo")
                .eq("userId", token.getUuid())
                .eq("deleted", false)
                .like(jsonObject, "name")
                .exe();
        return Result.success(wordsList);
    }

    /**
     * 新增个人话术
     * 暂时不支持属性接口
     * @param token 用户信息
     * @param wordsOwned 个人话术对象
     * @return
     */
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/addWords")
    public Object addDiscourse(Token token, WordsOwned wordsOwned) {
        // if(Objects.nonEmpty(wordsOwned.getName()) && Objects){
        //
        // }

        wordsOwned.setUserId(token.getUuid());
        wordsOwned.setPid(Root.ROOT_ID);
        jdbcSession.insert(wordsOwned);
        return Result.success(wordsOwned);
    }

  /*  private JSONObject toJson(Folder folder) {
        final JSONObject jsonObject = new JSONObject();

        jsonObject.put("id", folder.getId());
        jsonObject.put("name", folder.getName());
        jsonObject.put("type", "1");

        return jsonObject;
    }*/

    //todo 返回数据是否需要进行转换需要测试
    JSONObject toJson(WordsShare discourse) {
        final JSONObject jsonObject = new JSONObject();

        jsonObject.put("id", discourse.getId());
        jsonObject.put("name", discourse.getName());
        jsonObject.put("type", "3");

        return jsonObject;
    }

    //todo 返回数据是否需要进行转换需要测试
    JSONObject toJson(WordsOwned discourse) {
        final JSONObject jsonObject = new JSONObject();

        jsonObject.put("id", discourse.getId());
        jsonObject.put("name", discourse.getName());
        jsonObject.put("type", "3");

        return jsonObject;
    }
}
